import {
  validateCustomTwoDecimal,
  validateCustomPositiveInt,
  validateCustomADecimal
} from '@/utils/validate.js'
export default {
  data() {
    // 备货
    const validatorPrepayTime = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入时间'))
      } else if (!validateCustomPositiveInt(value, 3, [0])) {
        callback(new Error('请输入0，正整数'))
      } else {
        callback()
      }
    }
    // 预约
    const validatorBookingDays = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请输入时间'))
      } else if (!validateCustomPositiveInt(value, 2, [0])) {
        callback(new Error('请输入0，正整数'))
      } else if (validateCustomPositiveInt(this.form.prepayTime, 3, [0])) {
        let hours = 0
        if (this.form.prepayTimeUnit === 'DAYS') {
          hours = this.form.prepayTime * 24
        } else if (this.form.prepayTimeUnit === 'HOURS') {
          hours = this.form.prepayTime
        }
        value = value == 0 ? 1 : value
        if (value * 24 <= hours) {
          callback(new Error('可预约时间需大于备货时间'))
        } else {
          callback()
        }
      } else {
        callback()
      }
    }
    return {
      rules: {
        prepayTimeUnit: [
          {
            required: true,
            message: '请选择备货规则',
            trigger: ['blur', 'change']
          }
        ],
        prepayTime: [
          {
            required: true,
            validator: validatorPrepayTime,
            trigger: ['blur', 'change']
          }
        ],
        bookingDays: [
          {
            required: true,
            validator: validatorBookingDays,
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  }

}
